<?php

class Good
{

    static $sku_attr_info;

    /** 根据sku获取货品的属性图片
     * @param $sku
     * @return string
     */
    public function get_sku_attr_img($sku){
        $CI=&get_instance();
        $CI->load->model('goods_model');
        $CI->load->model('goods_sku_model');

        /** @var Goods_model  $good_model */
        $good_model = $CI->goods_model;

        /** @var goods_sku_model $good_sku_model */
        $good_sku_model = $CI->goods_sku_model;

        $sku_data = $good_sku_model->j_row(array('sku_num_sn' => $sku), 'goods_id');

        $goods_data = $good_model->j_row(array('goods_id' => $sku_data['goods_id']), 'goods_attr');

        $attr_data = json_decode($goods_data['goods_attr'], 1);

        //格式化每行的树形数据
        $format_attr_value = array();
        $count_value = count($attr_data['attr_val']);

        foreach($attr_data['attr_val'][0] as $key => $value){
            $format_attr_value_row = array();
            for($i = 0; $i < $count_value; $i++){
                $format_attr_value_row[] =  $attr_data['attr_val'][$i][$key];
            }

            $format_attr_value[] = $format_attr_value_row;
        }

        $attr_array = array_merge(array($attr_data['attr_title']), $format_attr_value);

        $CI->load->library('img');
        /** @var Img $img_lib */
        $img_lib = $CI->img;

        $file_str = 'data'.DIRECTORY_SEPARATOR.'attr'.DIRECTORY_SEPARATOR.$sku.'.jpg';
        $path_dir = FCPATH.$file_str;

        $url = config_item('base_url').$file_str.'?'.time();
        $img_lib->create_array_text_img($attr_array, $path_dir);


        return $url;
    }

    /** 获取商品分类的数量
     * @param $data
     * @return array
     */
    public function get_cat_cache_count($data){
        $CI = &get_instance();
        $CI->load->model('goods_sku_model');

        $CI->load->model('goods_model');

        $CI->load->model('category_model');
        foreach($data as &$value){
            $child_data = $CI->category_model->j_list(array('pid' => $value['cate_id']), 'cate_id');

            $child_data = $child_data ? : array();

            $cat_ids = array_merge(array($value['cate_id']), array_map('current', $child_data));
            $value['count'] = $CI->goods_model->j_count(array('cat_id' => $cat_ids));
        }

        return $data;
    }


    /** 创建可用的sku值
     * @param $info         商品信息
     * @param $attr_info    属性信息
     * @return string       sku值
     */
    public function create_sku($info, $attr_info,$type_id = 0){
        $goods_model = get_model('goods_model');

        $data = $goods_model->j_row(['goods_sn' => $info['goods_sn']], 'goods_id');

        if($data){
            $goods_sku_model = get_model('goods_sku_model');

            $sku_list = $goods_sku_model->j_list(['goods_id' => $data['goods_id']], 'sku_num_sn');
            foreach($sku_list as $value){
                self::$sku_attr_info[$value['sku_num_sn']] = true;
            }
        }

        $size_model = get_model('size_model');
        $size_info = $size_model->j_row(['size_value' => $attr_info['size']], 'new_size_code');

        $color_model = get_model('color_model');
        $color_info = $color_model->j_row(['color_en' => $attr_info['color']], 'new_color_code');
// echo $type_id;die;
        if(empty($type_id)){
            $type_id = sprintf("%03d", rand(0, 999));
        }else{
            $type_id = sprintf("%03d", $type_id);
        }
        $run = true;
        do{
            $sku = $info['goods_sn'].$type_id.$color_info['new_color_code'].$size_info['new_size_code'];
            if(!self::$sku_attr_info[$sku]){
                self::$sku_attr_info[$sku] = true;
                $run = false;
            }
        }while($run);

        if(empty($size_info) || empty($color_info) || strlen($sku) != 12){
            return false;
        }

        return $sku;
    }

}
